jQuery.extend({
    ALERT: function (msg, fn) {
        $.messager.alert('提示信息', msg, '', fn);
    },
    ERROR: function (msg, fn) {
        $.messager.alert('提示信息', msg, 'error', fn);
    },
    INFO: function (msg, fn) {
        $.messager.alert('提示信息', msg, 'info', fn);
    },
    QUESTION: function (msg, fn) {
        $.messager.alert('提示信息', msg, 'question', fn);
    },
    WARNING: function (msg, fn) {
        $.messager.alert('提示信息', msg, 'warning', fn);
    },
    CONFIRM: function (title, msg, fn) {
        $.messager.confirm(title, msg, function (r) {
            if (r) {
                fn();
            }
        });
    },
    selected: function (tableid) {
        var sel = $("#" + tableid).datagrid('getSelected');
        return sel;
    }
});


jQuery.extend({
    formLoad: function (formId, _url, callback) {
        $.ajax({
            url: _url,
            type: 'post',
            dataType: 'json',
            success: function (data) {
                $("#" + formId).form('load', data);
                if (callback) {
                    callback(data);
                }
            },
            error: function (data) {
                $.ALERT('加载数据失败！');
            }
        });
    },
    formClear: function (formId) {
        $('#' + formId).form('clear');
    }
});

/*打开一个新窗口*/
function openWindow(_url, _title, icons) {
    var win = "<div id=\"innerWindow\" class=\"easyui-window\">dfasfds</div>";
    $("body").append(win);
    $('#innerWindow').window({
        modal: true,
        title: _title,
        height: 300,
        width: 600,
        minimizable: false,
        maximizable: false,
        maximized: true,
        resizable: false,
        collapsible: false,
        draggable: false,
        iconCls: 'icon-save',
        inline: true
    });
    $('#innerWindow').window('refresh', _url);
}
/*关闭新窗口*/
function closeWindow() {
    $('#innerWindow').window('close');
    if (dataTable) {
        refreshParentTable();
    }
}
/*刷新父页面datagrid表格数据*/
function refreshParentTable() {
    dataTable.datagrid('reload');
}

/*保留两位小数*/
function changeTwoDecimal_f(floatvar) {
    var show = '0.00';
    if (floatvar == null || floatvar == '' || floatvar == undefined) {
        return show;
    }
    if (floatvar == 0.00 || floatvar == 0 || floatvar == 0.0) {    	
        return show;
    }
    var f_x = parseFloat(floatvar);
    if (isNaN(f_x)) {
        //alert('function:changeTwoDecimal->parameter error ');
        return show;
    }
    //var f_x_v = Math.round(f_x * 100) / 100;
    var f_x_v = parseInt(f_x);
    var s_x = f_x_v.toString();
    /*var pos_decimal = s_x.indexOf('.');
    if (pos_decimal < 0) {
        pos_decimal = s_x.length;
        s_x += '.';
    }
    while (s_x.length <= pos_decimal + 2) {
        s_x += '0';
    }*/
    return s_x + '.00';
}
//模拟表单提交
function postForm(URL, PARAMS) {
    var temp = document.createElement("form");
    temp.action = URL;
    temp.method = "post";
    temp.style.display = "none";
    for (var x in PARAMS) {
        var opt = document.createElement("textarea");
        opt.name = x;
        opt.value = PARAMS[x];
        // alert(opt.name)
        temp.appendChild(opt);
    }
    document.body.appendChild(temp);
    temp.submit();
    return temp;
}

//将date型转换为tring
//传来的datetime是:Wed Mar 04 2009 11:05:05 GMT+0800格式  得到结果：2009-06-12 17:18:05
function dateToStr(datetime) {
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1;//js从0开始取
    var date = datetime.getDate();
    var hour = datetime.getHours();
    var minutes = datetime.getMinutes();
    var second = datetime.getSeconds();
    if (month < 10) {
        month = "0" + month;
    }
    if (date < 10) {
        date = "0" + date;
    }
    if (hour < 10) {
        hour = "0" + hour;
    }
    if (minutes < 10) {
        minutes = "0" + minutes;
    }
    if (second < 10) {
        second = "0" + second;
    }
    var time = year + "-" + month + "-" + date + " " + hour + ":" + minutes + ":" + second; //2009-06-12 17:18:05
    return time;
}


//加法函数
function accAdd(arg1, arg2) {
    var r1, r2, m;
    try {
        r1 = arg1.toString().split(".")[1].length;
    }
    catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split(".")[1].length;
    }
    catch (e) {
        r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));
    return (arg1 * m + arg2 * m) / m;
}
//给Number类型增加一个add方法，，使用时直接用 .add 即可完成计算。
Number.prototype.add = function (arg) {
    return accAdd(arg, this);
};

//减法函数
function Subtr(arg1, arg2) {
    var r1, r2, m, n;
    try {
        r1 = arg1.toString().split(".")[1].length;
    }
    catch (e) {
        r1 = 0;
    }
    try {
        r2 = arg2.toString().split(".")[1].length;
    }
    catch (e) {
        r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));
    //last modify by deeka
    //动态控制精度长度
    n = (r1 >= r2) ? r1 : r2;
    return ((arg1 * m - arg2 * m) / m).toFixed(n);
}

//给Number类型增加一个add方法，，使用时直接用 .sub 即可完成计算。
Number.prototype.sub = function (arg) {
    return Subtr(this, arg);
};

//乘法函数
function accMul(arg1, arg2) {
    var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
    try {
        m += s1.split(".")[1].length;
    }
    catch (e) {
    }
    try {
        m += s2.split(".")[1].length;
    }
    catch (e) {
    }
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}
//给Number类型增加一个mul方法，使用时直接用 .mul 即可完成计算。
Number.prototype.mul = function (arg) {
    return accMul(arg, this);
};

//除法函数
function accDiv(arg1, arg2) {
    var t1 = 0, t2 = 0, r1, r2;
    try {
        t1 = arg1.toString().split(".")[1].length;
    }
    catch (e) {
    }
    try {
        t2 = arg2.toString().split(".")[1].length;
    }
    catch (e) {
    }
    with (Math) {
        r1 = Number(arg1.toString().replace(".", ""));
        r2 = Number(arg2.toString().replace(".", ""));
        return (r1 / r2) * pow(10, t2 - t1);
    }
}
//给Number类型增加一个div方法，，使用时直接用 .div 即可完成计算。
Number.prototype.div = function (arg) {
    return accDiv(this, arg);
};